#include <iostream>
#include <string.h>
//#include <locale.h>
//#include <langinfo.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

//ñ en utf32 es 241 o 0xf1
int	utf8_decode(char *, unsigned int &len,unsigned int despl);
int main(){
	FILE *f;
	char B[50];
	unsigned int leidos, despl = 0, len = 0;
	wchar_t caracter;
	f = fopen("archivo","rb");
	leidos = fread(B,1,50,f);
	for(int i=0;(B[despl] != 10) && (B[despl] != 0);i++){
//		printf("leidos: %d\n",i);
		caracter = utf8_decode(B, len, despl);
//		printf("largo del caracter(bytes): %d\n",len);
		if(len == 1)printf("el caracter es ASCII: %c\n",caracter);
		else printf("no es ASCII: %x\n",caracter);
		despl += len;
		len = 0;
	}
}


int	utf8_decode(char *B, unsigned int &len, unsigned int despl){
	int c = 0, k;
	unsigned char b;
	b = B[despl]; /*first byte of a B*/
	if ((b & 0x80) == 0x00)
		c = b, len = 0;
	else if ((b & 0xe0) == 0xc0)
		c = b & 0x1f, len = 1;
	else if ((b & 0xf0) == 0xe0)
		c = b & 0x0f, len = 2;
	else if ((b & 0xf8) == 0xf0)
		c = b & 0x07, len = 3;
	for (k = 0; k < len; ++k) {
		c <<= 6;
		b = B[despl+k+1];
		c |= (b & 0x3f);
	}
	//Para que len contenga el largo en bytes
	len+=1;
	return c;

}


